package com.app.twth.ui.adjust;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.app.twth.app.Constant;
import com.app.twth.base.BaseAndroidViewModel;
import com.app.twth.bean.BaseResponseEntity;
import com.app.twth.bean.LoadingBean;
import com.app.twth.net.JsonCallBack;
import com.app.twth.ui.adjust.lpn.AdjustLpnQr;
import com.app.twth.ui.adjust.remeasure.ReMeasureQr;
import com.app.twth.ui.adjust.retally.ReTally;
import com.app.twth.ui.tally.lpn.TalliedSku;
import com.lzy.okgo.model.Response;

import java.util.List;


/**
 * ****************************************************************
 * 文件名称: LoginViewModel
 * 文件描述:
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class AdjustViewModel extends BaseAndroidViewModel<AdjustDataModel> {
    public MutableLiveData<String> code = new MutableLiveData<>();
    public MutableLiveData<ReTally> reTallyLiveData = new MutableLiveData<>();
    public MutableLiveData<ReMeasureQr> reMeasureLiveData = new MutableLiveData<>();
    public MutableLiveData<ReTally> LpnSkuLiveData = new MutableLiveData<>();
    public MutableLiveData<TalliedSku> talliedSkuLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> submitLiveData = new MutableLiveData<>();

    public AdjustViewModel(@NonNull Application application) {
        super(application);
    }

    public void tallyReTally() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.tallyReTally(code.getValue(), new JsonCallBack<BaseResponseEntity<ReTally>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ReTally>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ReTally> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    reTallyLiveData.setValue(baseResponseEntity.data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void tallyReMeasure() {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        ReMeasureQr reMeasureQr = new ReMeasureQr();
        reMeasureQr.setBarcode(code.getValue());
        mModel.tallyReMeasure(reMeasureQr, new JsonCallBack<BaseResponseEntity<ReMeasureQr>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<ReMeasureQr>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<ReMeasureQr> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    ReMeasureQr data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    data.setBarcode(code.getValue());
                    reMeasureLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void adjustLpn(String lpn) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.adjustLpn(lpn, new JsonCallBack<BaseResponseEntity<List<TalliedSku>>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<List<TalliedSku>>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<List<TalliedSku>> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    List<TalliedSku> data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    ReTally tally = new ReTally();
                    tally.setIbLpn(lpn);
                    tally.setSkuList(data);
                    LpnSkuLiveData.setValue(tally);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void adjustInfo(String ibLpn, String barcode) {
        isLoading.setValue(new LoadingBean(true, Constant.loading));
        errorResultCode.setValue("");
        mModel.adjustInfo(ibLpn, barcode, new JsonCallBack<BaseResponseEntity<TalliedSku>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<TalliedSku>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<TalliedSku> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    TalliedSku data = baseResponseEntity.data;
                    if (data == null) {
                        return;
                    }
                    talliedSkuLiveData.setValue(data);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }

    public void adjustInfo(AdjustLpnQr adjustLpnQr) {
        isLoading.setValue(new LoadingBean(true, Constant.load_submission));
        errorResultCode.setValue("");
        mModel.adjustInfo(adjustLpnQr, new JsonCallBack<BaseResponseEntity<TalliedSku>>() {
            @Override
            public void onSuccessData(Response<BaseResponseEntity<TalliedSku>> response) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                BaseResponseEntity<TalliedSku> baseResponseEntity = response.body();
                if (baseResponseEntity == null) {
                    return;
                }
                if (baseResponseEntity.getCode() == null) {
                    return;
                }
                if (baseResponseEntity.getCode().equals("200")) {
                    submitLiveData.setValue(true);
                    return;
                }
                infoResultMsg = baseResponseEntity.getMessage();
                errorResultCode.setValue(infoResultMsg);
            }

            @Override
            public void onError(String error) {
                isLoading.setValue(new LoadingBean(false, Constant.loading));
                infoResultMsg = error;
                errorResultCode.setValue(infoResultMsg);
            }
        });
    }


}
